package com.gykfbxxh.imovie.manager;

import android.app.Activity;
import android.content.Context;

import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmPingActivity;

import java.util.Stack;

/**
 * @类名称：AppManager
 * @类描述： 应用程序Activity管理类：用于Activity管理和应用程序退出
 * Created by xuxinghai
 * On 2017/8/17
 */
public class AppManager {

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }


    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        if (activityStack == null || activityStack.size() == 0) {
            return;
        }
        Activity activity = activityStack.lastElement();
        if (activity != null) {
            if (!activity.isFinishing()) {
                finishActivity(activity);
            } else {
                activityStack.remove(activity);
                finishActivity();
            }
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity getLastActivity() {
        if (activityStack == null || activityStack.size() == 0) {
            return null;
        }
        return activityStack.lastElement();
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && activityStack != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (activityStack == null || cls == null) return;
        for (Activity activity : activityStack) {
            if (cls.equals(activity.getClass())) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 判断指定类名的activity是否存在
     *
     * @param cls
     * @return
     */
    public boolean isHaveActivity(Class<?> cls) {
        if (activityStack == null) return false;
        for (Activity activity : activityStack) {
            if (activity.getClass().getName().equals(cls.getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 结束指定某个Activity之上的页面(返回到某个Activity页面)
     */
    public void finishAllActivityByActivityName(Class<?> cls) {
        if (activityStack == null || cls == null) return;
        for (int i = activityStack.size() - 1; i >= 0; i--) {
            Activity ac = activityStack.get(i);
            if (null != ac && !cls.equals(ac.getClass())) {
                activityStack.get(i).finish();
                activityStack.remove(i);
            } else {
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityStack == null) return;
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
//        MobclickAgent.onKillProcess(context);
        finishAllActivity();
//        ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//        activityMgr.killBackgroundProcesses(context.getPackageName());
//        System.exit(0);
    }


    /*********************************
     * 新闻详情栈
     *********************************************/


    private static Stack<FilmDetailActivity> detailActivityStack;//新闻详情栈

    public void addDetailActivity(FilmDetailActivity detailActivity) {
        if (detailActivityStack == null) {
            detailActivityStack = new Stack<FilmDetailActivity>();
        }
        detailActivityStack.add(detailActivity);
    }


    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void removeLastDetailActivity() {
        if (detailActivityStack == null || detailActivityStack.size() == 0) {
            return;
        }
        FilmDetailActivity detailActivity = detailActivityStack.lastElement();
        detailActivityStack.remove(detailActivity);

    }

    public void finishFirstDetailActivity() {
        if (detailActivityStack == null || detailActivityStack.size() == 0) {
            return;
        }
        FilmDetailActivity detailActivity = detailActivityStack.firstElement();
        if (detailActivity != null) {
            if (!detailActivity.isFinishing()) {
                finishDetailActivity(detailActivity);
                if(activityStack != null && activityStack.size()> 0){
                    activityStack.remove(detailActivity);
                }
            } else {
                detailActivityStack.remove(detailActivity);
                finishFirstDetailActivity();
            }
        }
    }


    /**
     * 结束指定的Activity
     */
    public void finishDetailActivity(FilmDetailActivity activity) {
        if (activity != null && detailActivityStack != null && detailActivityStack.size() > 0) {
            detailActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    public int getDetailActivityCount() {
        if (detailActivityStack == null || detailActivityStack.size() == 0) {
            return 0;
        }


        return detailActivityStack.size();
    }


    /**
     * 影评详情栈
     */

    private static Stack<FilmPingActivity> pingActivityStack;//新闻详情栈

    public void addPingActivity(FilmPingActivity detailActivity) {
        if (pingActivityStack == null) {
            pingActivityStack = new Stack<FilmPingActivity>();
        }
        pingActivityStack.add(detailActivity);
    }


    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void removeLastPingActivity() {
        if (pingActivityStack == null || pingActivityStack.size() == 0) {
            return;
        }
        FilmPingActivity detailActivity = pingActivityStack.lastElement();
        pingActivityStack.remove(detailActivity);

    }

    public void finishFirstPingActivity() {
        if (pingActivityStack == null || pingActivityStack.size() == 0) {
            return;
        }
        FilmPingActivity detailActivity = pingActivityStack.firstElement();
        if (detailActivity != null) {
            if (!detailActivity.isFinishing()) {
                finishPingActivity(detailActivity);
                if(activityStack != null && activityStack.size()> 0){
                    activityStack.remove(detailActivity);
                }
            } else {
                pingActivityStack.remove(detailActivity);
                finishFirstPingActivity();
            }
        }
    }


    /**
     * 结束指定的Activity
     */
    public void finishPingActivity(FilmPingActivity activity) {
        if (activity != null && pingActivityStack != null && pingActivityStack.size() > 0) {
            pingActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    public int getPingActivityCount() {
        if (pingActivityStack == null || pingActivityStack.size() == 0) {
            return 0;
        }

        return pingActivityStack.size();
    }


}